package com.studentManagement.daoImpl;

import com.studentManagement.dao.TeacherDao;
import com.studentManagement.po.Teacher;
import com.studentManagement.utils.JdbcUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {
    private Connection conn = JdbcUtil.getConnection();

    @Override
    /**
     * 方法的功能描述：通过教师id查询教师的所有信息
     * @params：[teacher_id]
     * @return: com.studentManagement.po.Teacher */
    public Teacher queryTeacherByTId(String teacher_id) throws Exception {
        QueryRunner qr = new QueryRunner();
        String sql = "SELECT * FROM teachers WHERE  teacher_id=?";
        Object obj = qr.query(conn, sql, new BeanHandler(Teacher.class), teacher_id);
        DbUtils.close(conn);
        return (Teacher) obj;
    }

    @Override
    /**
     * 方法的功能描述：查询所有教师的所有信息
     * @params：[]
     * @return: java.util.List<com.studentManagement.po.Teacher> */
    public List<Teacher> queryAllTeacher() throws SQLException {
        QueryRunner qr=new QueryRunner();
        String sql="select * from teachers";
        Object result =qr.query(conn,sql,new BeanListHandler(Teacher.class));
        DbUtils.close(conn);
        return (List<Teacher>) result;
    }

    @Override
    /**
     * 方法的功能描述：添加教师
     * @params：[teacher]
     * @return: boolean */
    public boolean addTeacher(Teacher teacher) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "INSERT INTO teachers (\n" +
            "\tteacher_id,\n" +
            "\tteacher_key,\n" +
            "\tteacher_name,\n" +
            "\tteacher_sex,\n" +
            "\tteacher_age,\n" +
            "\tteacher_phone,\n" +
            "\tteacher_email,\n" +
            "\tteacher_note,\n" +
            "\tteacher_image_Path,\n" +
            "\tteacher_state,\n" +
            "\tteacher_uuid\n" +
            ")\n" +
            "VALUES\n" +
            "\t(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = {teacher.getTeacher_id(), teacher.getTeacher_key(), teacher.getTeacher_name(), teacher.getTeacher_sex(), teacher.getTeacher_age(), teacher.getTeacher_phone(), teacher.getTeacher_email(), teacher.getTeacher_note(), teacher.getTeacher_image_Path(), teacher.getTeacher_state(), teacher.getTeacher_uuid()};
        Object result = null;
        try {
            result = qr.update(conn, sql, params);

        } catch (Exception e) {
            return false;
        } finally {
            DbUtils.close(conn);
        }
        return true;

    }

    @Override
    /**
     * 方法的功能描述：更新教师信息
     * @params：[teacher, teacher_id]
     * @return: boolean */
    public boolean updateTeacher(Teacher teacher, String teacher_id) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "UPDATE teachers\n" +
            "SET teacher_id = ?,\n" +
            " teacher_key = ?,\n" +
            " teacher_name =   ?,\n" +
            " teacher_sex =  ?,\n" +
            " teacher_age =  ?,\n" +
            " teacher_phone =  ?,\n" +
            " teacher_email =  ?,\n" +
            " teacher_note = ?,\n" +
            " teacher_image_Path =  ?,\n" +
            " teacher_state =  ?,\n" +
            " teacher_uuid = ?\n" +
            "WHERE\n" +
            "\tteacher_id = ?";
        Object[] params = {teacher.getTeacher_id(), teacher.getTeacher_key(), teacher.getTeacher_name(),
            teacher.getTeacher_sex(), teacher.getTeacher_age(), teacher.getTeacher_phone(),
            teacher.getTeacher_email(), teacher.getTeacher_note(), teacher.getTeacher_image_Path(),
            teacher.getTeacher_state(), teacher.getTeacher_uuid(), teacher_id};
        Object result = qr.update(conn, sql, params);
        DbUtils.close(conn);
        if(result.toString().equals("1")) return true;
        else return false;
    }

    @Override
    /**
     * 方法的功能描述：通过教师uuid查询教师的所有信息
     * @params：[uuid]
     * @return: com.studentManagement.po.Teacher */
    public Teacher queryTeacher(String uuid) throws SQLException {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from teachers where teacher_uuid=?";
        Object result = qr.query(conn, sql, new BeanHandler(Teacher.class), uuid);
        DbUtils.close(conn);
        return (Teacher) result;
    }


}
